/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package UserPackage;

import DBConnection.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 *
 * @author CCS
 */
public class UserManager {

    public boolean checkUser(String username, String password)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
            String hash = new Hasher().passHasher("password");
    
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE userUsername = ? AND userPassword = ? AND userHash = ? AND userType = '4'");

            pstmt.setString(i++, username);
            pstmt.setString(i++, password);
            pstmt.setString(i++, hash);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result=true;
            }
            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
    
    public boolean checkProdMgr(String username, String password)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
    
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE userUsername = ? AND userPassword = ? AND userType = '2'");

            pstmt.setString(i++, username);
            pstmt.setString(i++, password);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result=true;
            }
            conn.close();
        }
        catch(SQLException ex)
            
        { ex.printStackTrace();}

        return result;

    }
    
    public boolean checkAcctMgr(String username, String password)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
    
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE userUsername = ? AND userPassword = ? AND userType = '3'");

            pstmt.setString(i++, username);
            pstmt.setString(i++, password);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result=true;
            }
            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
    
    

    public boolean checkAdmin(String username, String password)
    {
        boolean result=false;
    
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE userUsername = ? AND userPassword = ? AND userType = '1' ");

            pstmt.setString(i++, username);
            pstmt.setString(i++, password);
            
            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result=true;
            }
            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
     public boolean checkOldPass(String username, String password)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
            String hash = new Hasher().passHasher("password");
    
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE userUsername = ? AND userPassword = ? AND userHash = ?");

            pstmt.setString(i++, username);
            pstmt.setString(i++, password);
            pstmt.setString(i++, hash);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result=true;
            }
            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
    public boolean checkEmail(String eAdd)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
    
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM user WHERE userEmailAdd = ?");

            pstmt.setString(i++, eAdd);
           

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result=true;
            }
            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
    
     public boolean changeOldPass(User newUser) throws ParseException
    {
        boolean result = false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("UPDATE user set userPassword = ? where userUsername = ? ");
            int i =1;
            
            pstmt.setString(i++, newUser.getNewPass());
            pstmt.setString(i++, newUser.getUsername());
      
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(SQLException ex)
        { ex.printStackTrace();}
    
        return result;
    }

    public boolean addProdMgr(User newUser) throws ParseException
    {
        boolean result = false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO user(userFirstName,userMidName,userLastName,userEmailAdd,userUsername,userPassword,userType) VALUES(?,?,?,?,?,?,?)");
            int i =1;
            
            pstmt.setString(i++, newUser.getfName());
            pstmt.setString(i++, newUser.getmName());
            pstmt.setString(i++, newUser.getlName());
            //pstmt.setString(i++, newUser.getAddress());
            pstmt.setString(i++, newUser.getEmail());
            pstmt.setString(i++, newUser.getUsername());
            pstmt.setString(i++, newUser.getPassword());
            pstmt.setInt(i++, 2);
            //pstmt.setString(i++, newUser.getHash());
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(SQLException ex)
        { ex.printStackTrace();}
    
        return result;
    }
    
        public boolean addAcctMgr(User newUser) throws ParseException
    {
        boolean result = false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO user(userFirstName,userMidName,userLastName,userEmailAdd,userUsername,userPassword,userType) VALUES(?,?,?,?,?,?,?)");
            int i =1;
            
            pstmt.setString(i++, newUser.getfName());
            pstmt.setString(i++, newUser.getmName());
            pstmt.setString(i++, newUser.getlName());
            //pstmt.setString(i++, newUser.getAddress());
            pstmt.setString(i++, newUser.getEmail());
            pstmt.setString(i++, newUser.getUsername());
            pstmt.setString(i++, newUser.getPassword());
            pstmt.setInt(i++, 3);
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(SQLException ex)
        { ex.printStackTrace();}
    
        return result;
    }
    
    public boolean addUser(User newUser)throws ParseException
    {
            boolean result=false;

        try
        {
            //Opens DB Connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            //String dates= newUser.getccexpyr()+"-"+newUser.getccexpmon()+"-"+newUser.getccexpday();
            //java.util.Date d;
            //DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
            //d = (java.util.Date)formatter.parse(dates);
            //java.sql.Date ccexpdate = new java.sql.Date(d.getTime());

            


            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO user(userFirstName,userMidName,userLastName,userEmailAdd,userUsername,userPassword,userType,userHash) VALUES(?,?,?,?,?,?,?,?)");
            //include parameters
            int i=1, j =1, k = 1;

            
            pstmt.setString(i++, newUser.getfName());
            pstmt.setString(i++, newUser.getmName());
            pstmt.setString(i++, newUser.getlName());
            //pstmt.setString(i++, newUser.getAddress());
            pstmt.setString(i++, newUser.getEmail());
            pstmt.setString(i++, newUser.getUsername());
            pstmt.setString(i++, newUser.getPassword());
            //pstmt.setString(i++, newUser.getphoneNo());
            //pstmt.setString(i++, "No");
            //pstmt.setString(i++, newUser.getccnum());
            //pstmt.setString(i++, newUser.getccname());
            //pstmt.setString(i++, newUser.getcctype());
            //pstmt.setDate(i++, ccexpdate);
            
            //Execute sql statement
            pstmt.setInt(i++, 4);
            pstmt.setString(i++, newUser.getHash());
            pstmt.executeUpdate();
            //pstmt1.executeUpdate();
            
            
          /* 
            PreparedStatement pstmt1 = conn.prepareStatement("INSERT into creditcard(creditCardNumber, creditCardName, creditCardType, creditCardExp) VALUES (?,?,?,?)");
            pstmt1.setString(j++, newUser.getccnum());
            pstmt1.setString(j++, newUser.getccname());
            pstmt1.setString(j++, newUser.getcctype());
            pstmt1.setDate(j++, ccexpdate);
            
            
            pstmt1.executeUpdate();
            */

            //close SQL connection
            conn.close();
            
            result= true;
            

        }
        catch(SQLException ex)
        { ex.printStackTrace();}
    
        return result;
    }    
     public boolean addLogAttempt(String username)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
           
            PreparedStatement pstmt = conn.prepareStatement("UPDATE user set userLogInStatus = userLogInStatus + 1 where userUsername = ? ");
            pstmt.setString(i++, username);
            
            
            pstmt.executeUpdate();

            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
     public boolean resetLogAttempt(String username)
    {
        boolean result=false;
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
           
    
            PreparedStatement pstmt = conn.prepareStatement("UPDATE user set userLogInStatus = '0' where userUsername = ? ");
            pstmt.setString(i++, username);
            
            
            pstmt.executeUpdate();

            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return result;

    }
     public int checkLogAttempt(String username)
    {    int logattempt = 0;
        
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
           
    
            PreparedStatement pstmt = conn.prepareStatement("SELECT userLogInStatus from user where userUsername = ?");
            pstmt.setString(i++, username);
            
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            logattempt = rs.getInt("userLogInStatus");
     
            
            
            
            
            conn.close();
        }
        catch(SQLException ex)
        { ex.printStackTrace();}

        return logattempt;

    }
     
     public boolean checkUserExists(String username) {
        boolean result = false;
        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;
            PreparedStatement pstat = conn.prepareStatement("SELECT * FROM user WHERE userUsername = ?");
            pstat.setString(i++, username);

            ResultSet rs = pstat.executeQuery();

            while (rs.next()) {
                result = true;
            }
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }


        return result;
    }

    public boolean checkEmailExists(String email) {
        boolean result = false;
        try {
           ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i=1;
            PreparedStatement pstat = conn.prepareStatement("SELECT * FROM user WHERE userEmailAdd = ?");
            pstat.setString(i++, email);

            ResultSet rs = pstat.executeQuery();

            while (rs.next()) {
                result = true;
            }
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }


        return result;
    }
}
